****************************************************************************************************************************
Date: 18 SEP 2014

Purpose: Add maternity leave variable

****************************************************************************************************************************;

**************************************************************
*Indhenter min fulde sample

**************************************************************;
libname in1 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data nicu_data;
set in1.nicu_divorce;
birthmonth=month(fodtdato);
run;



*Begrænser sample til de relevante kohorter (plus lidt);
data nicu_data1;
set nicu_data (keep= pnr pnrm pnrf fodtdato birthyear birthmonth);

if birthyear<1984 then delete;
if birthyear>2002 then delete;
run;

proc sort data=nicu_data1;
by pnrm fodtdato;
run;


************************************************
Indlæser sammenhængende social statistik data
************************************************;

libname raw1 'F:\Rawdata\702727\data200811';

*Laver en makro;

%macro shss(start,slut);
%do i=&start %to &slut;

data shss&i;
set raw1.shss_x1&i;

%do z=1 %to 12;

%let t=%eval((&i.-1984)*12+&z.);
rename varmmsf&z.=var&t.;
%end;

drop belobf;

rename pnr=pnrm;

proc sort;
by pnrm;
run;
%end; 
proc contents;
%mend;
%shss(1984, 2005);
run;


data shss; 
merge shss1984 shss1985 shss1986 shss1987 shss1988 shss1989 shss1990 shss1991 shss1992 shss1993 shss1994 
shss1995 shss1996 shss1997 shss1998 shss1999 shss2000 shss2001 shss2002 shss2003 shss2004 shss2005;
by pnrm;
run;


*********************
Fødselsdato
*********************;
data fodt;
set nicu_data1;

fodt=(birthyear-1984)*12+birthmonth;
run;


data barsel;
merge fodt (in=a) shss;
by pnrm;
if a;
run;

proc freq data=barsel;
table fodt;
run;

data barsel1;
set barsel;

array temp_var {264} var1-var264;
leave=0;
do i=fodt to fodt+12;
leave=leave+temp_var{i};
end;
run;

proc means data=barsel1;
var leave;
run;


data test;
set barsel1;

if fodt>150 then output test;
run;

proc print data=test(obs=100);
var var140-var200 fodt leave;
run;





****************************************
kan nu koble leave på mit endelig data
*****************************************;

data barsel2;
set barsel1 (drop=birthmonth fodt var: i);
proc sort;
by pnr;
run;

proc sort data=nicu_data (drop=birthmonth);
by pnr;
run;

data nicu_maternity;
merge nicu_data (in=a) barsel2;
by pnr;
if a;
run;


data test;
set nicu_maternity;

if birthyear<1982 then delete;
if birthyear>1993 then delete;

if bw<1400 then delete;
if bw>1600 then delete;

if ga<32 then delete;
run;

proc means data=test;
var leave;
run;
 
*SAS datasæt;
libname out 'K:\Data\Workdata\702727\xru\NICU project\Datasæt';

data out.nicu_maternity;
set nicu_maternity;
run;

